/*
 * @lc app=leetcode.cn id=128 lang=javascript
 *
 * [128] 最长连续序列
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number}
 */
var longestConsecutive = function (nums) {
  const numSet = new Set(nums);
  let maxLen = 0;
  for (const val of numSet.keys()) {
    if (numSet.has(val - 1)) continue;
    let cnt = 1;
    let startNum = val;
    while (numSet.has(++startNum)) cnt++;
    maxLen = Math.max(cnt, maxLen);
  }
  return maxLen;
};
// @lc code=end
